//|||||||||||||||||||||||||||||||||||||||||||||||

#include <sstream>
#include "TipsMenuState2.hpp"
#include "GameApp.hpp"
#include "GameConstants.h"

//|||||||||||||||||||||||||||||||||||||||||||||||

using namespace Ogre;

//|||||||||||||||||||||||||||||||||||||||||||||||

TipsMenuState2::TipsMenuState2()
{
	m_FrameEvent = Ogre::FrameEvent();
	m_progress = new GameProgress();
//	m_bQuit = false;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::enter()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering TipsMenuState2...");

    m_pSceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "TipsMenu2SceneMgr");
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("TipsMenu2Cam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

    m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
        Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

    OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
//    OgreFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
	
	buildGUI();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::buildGUI()
{
	// top buttons
    OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_TOP, "Back2", "Back to Main Menu", 250);

	// bottom button
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_BOTTOM, "PreviousTip", "Go to Previous Tips", 250);
	
	// center intro text
	Ogre::String introTextTwo = "This menu displaye the tips and tricks you have collected to help you beat stress:\n\n";

	bool	tip1 = false;
	bool	tip2 = false;
	bool	tip3 = false;
	bool	tip4 = false;
	bool	tip5 = true;
	bool	tip6 = true;
	bool	tip7 = true;
	bool	tip18 = true;

	if (tip1){
		introTextTwo.append("1) Deal with problems on the spot\n\n");
		introTextTwo.append("Don't bottle up your feelings. They will just grow until they erupt. So if there are problems at\n");
		introTextTwo.append(" e.g. work, make sure that you deal with them on the spot.\n\n");
	}
	
	if (tip2){
		introTextTwo.append("2) Strong, confiding relationships\n\n");
		introTextTwo.append("Stress often makes us hard to live with. This can cause problems with those close to us.\n");
		introTextTwo.append(" But we know that strong relationships can help fight stress. So if there is a problem\n");
		introTextTwo.append("here, get to work on it.\n\n");
	}

	if (tip3){
		introTextTwo.append("3) Slow down\n\n");
		introTextTwo.append("Don't do things at 100 miles an hour. Eat, walk and drive more slowly. If you don't get\n");
		introTextTwo.append("as much done as you would like then it is no big deal. There is always another day.\n\n");
	}

	if (tip4){
		introTextTwo.append("4) One thing at a time\n\n");
		introTextTwo.append("Think of someone at work cradling a phone between his shoulder blade and ear.\n");
		introTextTwo.append("With one hand, he writes a letter and, with the other, searches through some papers. At the\n");
		introTextTwo.append("same time, he tries to grab a quick snack. He is overloading the system. If you make a phone\n");
		introTextTwo.append("call, make only the call and nothing else. The message: is don't keep too many balls\n");
		introTextTwo.append("in the air at the one time.\n\n");
	}

	if (tip5){
		introTextTwo.append("5) Divide problems up\n\n");
		introTextTwo.append("If you face a huge problem and can see no way to cope with it, see if you can divide\n");
		introTextTwo.append("it up into bite sized bits. Then tackle the bits one at a time.\n\n");
	}

	if (tip6){
		introTextTwo.append("6) Dont accept other peoples targets\n\n");
		introTextTwo.append("Do people expect too much of you? If you feel they do, confront this. Have a\n");
		introTextTwo.append("quiet word and try to sort this out. If you cant agree, say NO.\n\n");
	}

	if (tip7){
		introTextTwo.append("7) Confide in others\n\n");
		introTextTwo.append("Seek out those you can trust and let them know how you feel.\n");
		introTextTwo.append("They may be able to see ways out of problems that you have not thought about.\n");
		introTextTwo.append("In any case, it will help to get things off your chest.\n");
		introTextTwo.append("It may also help you feel less isolated.\n\n");
	}

	if (tip18){
		introTextTwo.append("8) Tablets\n\n");
		introTextTwo.append("Always discuss this with your GP. If you want to fight stress without\n");
		introTextTwo.append("pills, then that is fine. You might want to combine pills with \n");
		introTextTwo.append("self-help. Your GP might give you  diazepam (valium)  but usually for a few \n");
		introTextTwo.append("days only. You may take a  beta blocker such as Inderal and Propranolol.\n");
		introTextTwo.append("Your doctor may suggest you take anti-depressants. If you want\n");
		introTextTwo.append("to know more about these kinds of pills, ask us for a handout about\n");
		introTextTwo.append("them or get it from our website www.glasgowsteps.com But pills\n");
		introTextTwo.append("on their own may not get you far... pills dont teach you skills.\n");
	}

	OgreFramework::getSingletonPtr()->m_pTrayMgr->createTextBox(OgreBites::TL_CENTER, "Intro2", introTextTwo, 800, 500);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::exit()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving TipsMenuState2...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if(m_pSceneMgr)
        OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool TipsMenuState2::keyPressed(const OIS::KeyEvent &keyEventRef)
{
    if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
    {
        m_bQuit = true;
        return true;
    }

    OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool TipsMenuState2::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool TipsMenuState2::mouseMoved(const OIS::MouseEvent &evt)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(evt)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool TipsMenuState2::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool TipsMenuState2::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(evt, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::update(double timeSinceLastFrame)
{
    m_FrameEvent.timeSinceLastFrame = timeSinceLastFrame;
    OgreFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

    if(m_bQuit == true)
    {
        shutdown();
        return;
    }
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::buttonHit(OgreBites::Button *button)
{
	

	if(button->getName() == "ExitBtn") {
        m_bQuit = true;
	} else if(button->getName() == "Back2") {
		changeAppState(findByName("GameProgressMenuState"));
	} else if(button->getName() == "PreviousTip") {
		changeAppState(findByName("TipsMenuState"));
	}
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void TipsMenuState2::setProgress(GameProgress* progress)
{
	m_progress = progress;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

GameProgress* TipsMenuState2::getProgress()
{
	return m_progress;
}
